#include "add_dtv.h"
#include "ui_add_dtv.h"

Add_DTV::Add_DTV(int solution,QList<uint> *atv_num,QMap<uint,uchar> *dtv_air_num,QMap<uint,uchar> *dtv_cable_num,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Add_DTV)
{
    ui->setupUi(this);
    this->solution=solution;
    this->atv_num=atv_num;
    this->dtv_air_num=dtv_air_num;
    this->dtv_cable_num=dtv_cable_num;
    MyInit();
}

Add_DTV::~Add_DTV()
{
    delete ui;
}


void Add_DTV::MyInit()
{

    this->setStyleSheet("background-color:rgb(135,206,235)");
    ui->msg->setStyleSheet("color:red");
    ui->Item1->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item2->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item3->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item4->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item5->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item6->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->pB_Confirm->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item7->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->label->setText("Channel Number:");
    ui->label_2->setText("Channel Name");
    ui->label_3->setText("Channel Class");
    if(1==solution)
    {
        QStringList tmp;
                tmp<<"DTV-B"
                <<"DTV-C";
        ui->Item3->addItems(tmp);
    }else{
        QStringList tmp;
                tmp<<"DTV-T"
               <<"DTV-C"
              <<"DTV-S/S2"
             <<"DTV-T2";
        ui->Item3->addItems(tmp);
    }
    ui->label_4->setText("Channel Type");
    QStringList tmp;
            tmp<<"D"
           <<"R"
          <<"T"
         <<"U";
    ui->Item4->addItems(tmp);
    ui->label_5->setText("Channel Freq");
    ui->label_6->setText("Channel Favorite");
    tmp.clear();
    tmp.append("0");
    tmp.append("1");
    ui->Item6->addItems(tmp);
    ui->label_7->setText("Channel Skip");
    ui->Item7->addItems(tmp);
    connect(ui->pB_Confirm,SIGNAL(clicked()),this,SLOT(Confirm()));
    connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    connect(ui->Item5,SIGNAL(editingFinished()),this,SLOT(Check_Freq()));
}


void Add_DTV::Check_Num()
{
    CHECK_INPUT_EMPTY(ui->Item1->text());
    bool flag=false;
    bool *ok=&flag;
    uint num=ui->Item1->text().toUInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Channel Number Error!");
        ui->pB_Confirm->setEnabled(false);
        return ;
    }else{
        ui->msg->clear();
        ui->pB_Confirm->setEnabled(true);
    }
    ui->msg->clear();
}

void Add_DTV::Check_Freq()
{
    CHECK_INPUT_EMPTY(ui->Item5->text());
    bool Flag=false;
    bool *ok=&Flag;
    ui->Item5->text().toInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Frequency Error");
        ui->pB_Confirm->setEnabled(false);
    }else{
        ui->msg->clear();
        ui->pB_Confirm->setEnabled(true);
    }
}
int Add_DTV::Check_Num(uint num)
{

    bool flag=false;
    bool *ok=&flag;
    uint type;
    if(0==ui->Item4->currentIndex())
        type=1;
    else if(1==ui->Item4->currentIndex())
        type=2;
    else if(2==ui->Item4->currentIndex())
        type=3;
    else if(3==ui->Item4->currentIndex())
        type=4;
    else
        type=4;//error happened
    //Q_ASSERT(*ok==true);
    int ch_class=ui->Item3->currentIndex();
    if(atv_num&&atv_num->size()>0)
    {
        for(int i=0;i<atv_num->size();i++)
        {
            if(num==atv_num->at(i))
            {
                QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other atv channels.",QMessageBox::Ok);
                return -1;
            }
        }
    }

    if((solution==1&&ch_class==0&&dtv_air_num->size()>0)||(solution==0&&dtv_air_num->size()>0))
    {
        if(dtv_air_num->size()>0)
        {
            QMap<uint,uchar>::iterator i;
            for(i=dtv_air_num->begin();i!=dtv_air_num->end();++i)
            {
                uint ntmp=i.key();
                uchar ctmp=i.value();
                if(num==ntmp&&type==ctmp)
                {
                    QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    return -1;
                }
            }
        }else
        {
            QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            return -1;
        }
        dtv_air_num->insertMulti(num,type);
        QMap<uint,uchar>::iterator iter;

        qDebug("Add Data:");
        for(iter=dtv_air_num->begin();iter!=dtv_air_num->end();iter++)
            qDebug("ch_val :%d",iter.key());
    }else if(ch_class==1&&dtv_cable_num->size()>0&&solution==1)
    {
        if(dtv_cable_num->size()>0)
        {
            QMap<uint,uchar>::const_iterator i;
            //QList<uchar> type_list;
            for(i=dtv_cable_num->constBegin();i!=dtv_cable_num->constEnd();++i)
            {
                if(num==i.key()&&type==i.value())
                {
                    QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    //ui->Item1->setText(ch_data.at(0));
                    return -1;
                }
            }
        }else
        {
            QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            return -1;
        }


        dtv_cable_num->insertMulti(num,type);

    }else
    {
       // QMessageBox::warning(this,"error","QList DTV is NULL or the dtv data ,which you chose,its class out of range! ");
        //return -1;
    }
    return 1;
}

 int Add_DTV::Check(QList<QString>& Item_Data)
{
    if(Item_Data.size()>7)
    {
        QMessageBox::warning(this,"Warning","Item datas error");
        return -1;
    }
    bool Flag=false;
    bool *ok=&Flag;
    uint ch_val=Item_Data.at(0).toInt(ok);
    STRING_TRAN_ERROR_1(-1);
    if(-1==Check_Num(ch_val))
        return -1;

  return 1;
}



 void Add_DTV::Confirm(
         )
{

     QList<QString> Item_Data;
     QString tmp;
     tmp=ui->Item1->text();
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);
     tmp=ui->Item2->text();
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);

     tmp.clear();
     tmp.append(QString::number(ui->Item3->currentIndex()));
     Item_Data.append(tmp);

     tmp.clear();
     tmp.append(QString::number(ui->Item4->currentIndex()+1));
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);
     tmp.clear();
     tmp=ui->Item5->text();
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);
     tmp.clear();
     tmp.append(QString::number(ui->Item6->currentIndex()));
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);
     tmp.clear();
     tmp.append(QString::number(ui->Item7->currentIndex()));
     CHECK_INPUT_EMPTY(tmp);
     Item_Data.append(tmp);

     if(-1==Check(Item_Data))
     {
         return ;
     }


    if(0==solution)
    {
        TCL_MTK_DTV_TV *mtk_dtv=new TCL_MTK_DTV_TV;
        mtk_dtv->Set_Data(Item_Data);
        emit Add_MTK_DTV(mtk_dtv);

    }else{
        TCL_MSTAR_DTV_TV *mstar_dtv=new TCL_MSTAR_DTV_TV;
        mstar_dtv->Set_Data(Item_Data);
        emit Add_MSTAR_DTV(mstar_dtv);
    }
            //QMessageBox::warning(this,"tips","Add Success");
            ui->msg->setText("Add Success!");
            ui->Item1->clear();
            ui->Item2->clear();
}
